home *** CD-ROM | disk | FTP | other *** search
/ EnigmA Amiga Run 1995 October / EnigmA AMIGA RUN 01 (1995)(G.R. Edizioni)(IT)[!][issue 1995-10][Aminet 7].iso / Aminet / util / misc / cookies.lha / Cookie / onecookie.c < prev    next >
C/C++ Source or Header  |  1993-05-16  |  2KB  |  112 lines

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <exec/types.h>
  5. #include <exec/memory.h>
  6. #include <exec/io.h>
  7. #include <proto/exec.h>
  8.  
  9.  
  10. struct Cookie {
  11.     struct Cookie *next;
  12.     char *text;
  13. };
  14.  
  15.  
  16.  
  17. struct Cookie *clist = NULL;
  18. char cbuf[20000];    /* large enough to hold one complete cookie */
  19. char line[1024];    /* large enough to hold the longest line */
  20.  
  21.  
  22.  
  23. void read_cookies(FILE *fp)
  24. {
  25.     fprintf(stderr,"reading cookies"); fflush(stderr);
  26.     if (fp) {
  27.         struct Cookie *i;
  28.         long cbuflen;
  29.         if (i = clist = AllocVec(sizeof(struct Cookie),MEMF_CLEAR)) {
  30.             strcpy(cbuf,"");
  31.             while (fgets(line,1024,fp)) {
  32.                 if (strcmp(line,"%%\n")==0) {
  33.                     cbuflen = strlen(cbuf)+1;
  34.                     if (i->text = AllocVec(cbuflen,0L)) {
  35.                         CopyMem(cbuf,i->text,cbuflen);
  36.                     }
  37.                     if (!(i->next = AllocVec(sizeof(struct Cookie),MEMF_CLEAR))) {
  38.                         struct Cookie *l,*nxt;
  39.                         for (l=clist; l; l=nxt) {
  40.                             nxt = l->next;
  41.                             FreeVec(l->text);
  42.                             FreeVec(l);
  43.                         }
  44.                         fprintf(stderr,"failed memory allocation\n");
  45.                         exit(20);
  46.                     }
  47.                     i = i->next;
  48.                     strcpy(cbuf,"");
  49.                 }
  50.                 else {
  51.                     strcat(cbuf,line);
  52.                 }
  53.             }
  54.         }
  55.     }
  56.     fprintf(stderr,", done.\n");
  57. }
  58.  
  59.  
  60.  
  61. void write_cookies(FILE *fp)
  62. {
  63.     fprintf(stderr,"writing cookies"); fflush(stderr);
  64.     if (fp) {
  65.         struct Cookie *l,*nxt=NULL;
  66.         for (l=clist; l; l=nxt) {
  67.             if (l->text) {
  68.                 fwrite(l->text,strlen(l->text),1,fp);
  69.                 fwrite("%%\n",3,1,fp);
  70.                 FreeVec(l->text);
  71.             }
  72.             nxt = l->next;
  73.             FreeVec(l);
  74.         }
  75.     }
  76.     fprintf(stderr,", done.\n");
  77. }
  78.  
  79.  
  80.  
  81. void one_cookie(void)
  82. {
  83.     struct Cookie *l,*i;
  84.     long dbl = 0L;
  85.     fprintf(stderr,"removing double entries"); fflush(stderr);
  86.     for (l=clist; l; l=l->next) {
  87.         if (l->text) {
  88.             for (i=l->next; i; i=i->next) {
  89.                 if (i->text) {
  90.                     if (strcmp(l->text,i->text)==0) {
  91.                         FreeVec(i->text);
  92.                         i->text = NULL;
  93.                         ++dbl;
  94.                     }
  95.                 }
  96.             }
  97.         }
  98.     }
  99.     fprintf(stderr,", done. (%ld found)\n",dbl);
  100. }
  101.  
  102.  
  103.  
  104. int main(int argc, char *argv[])
  105. {
  106.     read_cookies(stdin);
  107.     one_cookie();
  108.     write_cookies(stdout);
  109.     exit(0);
  110. }
  111.  
  112.